package com.gzy.xuexiziliao.controller;

import com.gzy.xuexiziliao.common.context.BaseContext;
import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Student;
import com.gzy.xuexiziliao.service.StudentService;
import com.gzy.xuexiziliao.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 学生登录
 */
@RestController
@Slf4j
public class StudentLoginController {

    @Autowired
    private StudentService studentService;

//    @PostMapping("student/login")
//    public Result login(@RequestBody Student student) {
//        log.info("学生登录：{}", student);
//        Student a = studentService.login(student);
//        //获取jwt令牌
//        if (a != null) {
//            Map<String, Object> claims = new HashMap<>();
//            claims.put("studentId", a.getStudentId());
//            claims.put("name", a.getName());
//            claims.put("studentname", a.getStudentname());
//            String jwt = JwtUtils.generateJwt(claims);
//            return Result.success(jwt);
//        }
//        return Result.error("用户名或密码错误");
//    }
    @PostMapping("/student/login")
    public Result login(@RequestBody Student student) {
        log.info("学生登录：{}", student);
        Student stu = studentService.wxlogin(student);
        log.info("学生登录：{}", stu);

        //获取jwt令牌
        if (stu != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("studentId", stu.getStudentId());
            claims.put("name", stu.getName());
            claims.put("openid", stu.getOpenid());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }

}
